package com.xs.bookspoststation.UserActivity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.xs.bookspoststation.R;
import com.xs.bookspoststation.db.DBHelperUser;

public class AddUserActivity extends AppCompatActivity implements View.OnClickListener {
    //定义对象
    private EditText edit_add_username2,edit_add_userpwd2,edit_add_userphone2,user_education1,
            edit_add_fullname,edit_add_email,edit_add_province,edit_add_school;
    private RadioGroup user_add_radioGroup2;
    private RadioButton user_add_radioButton1,user_add_radioButton2;
    private CheckBox add_user_checkBox1,add_user_checkBox2,add_user_checkBox3;
    private Button btn_add_user_cancel1,btn_add_user1;
    //定义一个数据库帮助类
    private DBHelperUser dbHelperUser;
    //定义一个数据库操作类
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_user);

        //操作对象的方法
        initView();
    }


    private void initView() {
        //获取用户名控件
        edit_add_username2 = findViewById(R.id.edit_add_username2);
        //获取密码控件
        edit_add_userpwd2 = findViewById(R.id.edit_add_userpwd2);
        //获取电话控件
        edit_add_userphone2 = findViewById(R.id.edit_add_userphone2);
        //获取按钮
        user_add_radioGroup2 = findViewById(R.id.user_add_radioGroup2);
        //获取性别男控件
        user_add_radioButton1 = findViewById(R.id.user_add_radioButton1);
        //获取性别女控件
        user_add_radioButton2 = findViewById(R.id.user_add_radioButton2);
        //获取爱好控件
        add_user_checkBox1 = findViewById(R.id.add_user_checkBox1);
        //获取爱好控件
        add_user_checkBox2 = findViewById(R.id.add_user_checkBox2);
        //获取爱好控件
        add_user_checkBox3 = findViewById(R.id.add_user_checkBox3);
        //获取学历控件
        user_education1 = findViewById(R.id.user_education1);
        //获取姓名控件
        edit_add_fullname = findViewById(R.id.edit_add_fullname);
        //获取邮箱控件
        edit_add_email = findViewById(R.id.edit_add_email);
        //获取籍贯控件
        edit_add_province = findViewById(R.id.edit_add_province);
        //获取学校控件
        edit_add_school = findViewById(R.id.edit_add_school);
        //获取取消按钮控件
        btn_add_user_cancel1 = findViewById(R.id.btn_add_user_cancel1);
        //获取注册按钮控件
        btn_add_user1 = findViewById(R.id.btn_add_user1);
        //点击事件
        btn_add_user_cancel1.setOnClickListener(this);
        btn_add_user1.setOnClickListener(this);
        //实例化数据库帮助类对象
        dbHelperUser = new DBHelperUser(AddUserActivity.this,"books_station.db",null,1);
        //创建数据库并获取数据库的读写权限
        db = dbHelperUser.getWritableDatabase();
    }

    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add_user_cancel1:
                Intent intent=new Intent(AddUserActivity.this,OperateUserActivity.class);
                Toast.makeText(getApplicationContext(), "返回上一界面", Toast.LENGTH_LONG).show();
                startActivity(intent);
                break;
            case R.id.btn_add_user1:
                goAdd();
                break;
        }
    }

    /**
     * 添加
     */
    private void goAdd() {
        // 获取用户输入的数据
        String username = edit_add_username2.getText().toString().trim();
        String userpass = edit_add_userpwd2.getText().toString().trim();
        String userphone = edit_add_userphone2.getText().toString().trim();
        String usereducation1 = user_education1.getText().toString().trim();
        String addfullname = edit_add_fullname.getText().toString().trim();
        String add_email = edit_add_email.getText().toString().trim();
        String add_province = edit_add_province.getText().toString().trim();
        String add_school = edit_add_school.getText().toString().trim();

        //获取性别值
        String strSex = "";
        //根据选中的id进行判断
        switch (user_add_radioGroup2.getCheckedRadioButtonId()){
            case R.id.user_add_radioButton1:
                strSex = user_add_radioButton1.getText().toString();
                break;
            case R.id.user_add_radioButton2:
                strSex = user_add_radioButton2.getText().toString();
                break;
        }
        //获取爱好
        StringBuilder builder = new StringBuilder();//使用字符串拼接的时候可以用StringBuilder,它的长度是可变的；可以将你复选框汇总选中的内容添加进去
        if (add_user_checkBox1.isChecked()){
            builder.append(add_user_checkBox1.getText().toString());
        }
        if (add_user_checkBox2.isChecked()){
            builder.append(add_user_checkBox2.getText().toString());
        }
        if (add_user_checkBox3.isChecked()){
            builder.append(add_user_checkBox3.getText().toString());
        }
        String strHobby = builder.toString().trim();

        boolean AddUser = true;
        dbHelperUser = new DBHelperUser(AddUserActivity.this,"books_station.db",null,1);
        db = dbHelperUser.getWritableDatabase();
        if (username.equals("") || userpass.equals("") || userphone.equals("")){
            Toast.makeText(AddUserActivity.this, "您还有信息未填写！！！", Toast.LENGTH_SHORT).show();
        }else if(userpass.length() < 6 || userpass.length() > 6){
            Toast.makeText(AddUserActivity.this, "密码必须为六位数", Toast.LENGTH_SHORT).show();
        }else {
            Cursor cursor = db.query("book_user",new String[]{"username"},null,null,null,null,null);
            while (cursor.moveToNext()){
                if (username.equals(cursor.getString(cursor.getColumnIndex("username")))){
                    Toast.makeText(AddUserActivity.this, "该账号已存在", Toast.LENGTH_SHORT).show();
                    AddUser = false;
                    break;
                }
            }
            if (AddUser){
                //存储类，只能为String类型
                ContentValues values = new ContentValues();
                values.put("username",username);
                values.put("userpwd",userpass);
                values.put("userphone",userphone);
                values.put("usersex",strSex);
                values.put("userhobby",strHobby);
                values.put("usereducation",usereducation1);
                values.put("userfullname",addfullname);
                values.put("useremail",add_email);
                values.put("userprovince",add_province);
                values.put("userschool",add_school);
                db.insert("book_user",null,values);
                db.close();//关闭数据库
                AlertDialog alertDialog = new AlertDialog.Builder(AddUserActivity.this).create();
                alertDialog.setTitle("添加成功");
                alertDialog.setMessage("是否查看？");
                alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.show();
            }
        }
    }
}
